﻿using Microsoft.AspNetCore.Mvc;
using Zhp.Api.Attributes;
using Zhp.Auth;
using Zhp.IService.Chat;
using Zhp.Model.Chat;
using Zhp.Model.Common;

namespace Zhp.Api.Controllers.Chat
{
    [Route("api/[controller]/[action]")]
    [ApiGroup(GroupName = ApiGroupEnum.Chat)]
    [ApiController]
    [SeedMenu(MenuGroup = ApiGroupEnum.None, Name = "聊天大厅", Order = 3, RouterName = "chat", WebFilePath = "chat/index", Icon = "chat-2")]
    public class ChatMessageController : ControllerBase
    {
        private readonly IChatMessageService chatMessageSvc;
        public ChatMessageController(IChatMessageService chatMessageService)
        {
            chatMessageSvc = chatMessageService;
        }

        /// <summary>
        /// 查询聊天记录
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [AllowOnline]
        [HttpPost()]
        public async Task<ListRet<ChatMessageModel>> GetChatMessageListByUserId(QueryChatMessage query)
        {
            var (list, hasMore) = await chatMessageSvc.GetChatMessageListByUserId(query);

            return new ListRet<ChatMessageModel> { DataList = list, DataExt = hasMore };
        }

        /// <summary>
        /// 查询未读消息
        /// </summary>
        /// <returns></returns>
        [AllowOnline]
        [HttpGet()]
        public async Task<ListRet<ChatMessageModel>> GetChatMessageNoReadList()
        {
            var list = await chatMessageSvc.GetChatMessageNoReadList();

            return new ListRet<ChatMessageModel> { DataList = list };
        }


        /// <summary>
        /// 未读消息标记为已读
        /// </summary>
        /// <returns></returns>
        [AllowOnline]
        [HttpGet("{msgId}")]
        public async Task<bool> UpdateMsgReaded(long msgId)
        {
            return await chatMessageSvc.UpdateMsgReaded(msgId);
        }


        /// <summary>
        /// 发送消息前校验是否是好友，并且返回相关参数
        /// </summary>
        /// <returns></returns>
        [AllowOnline]
        [HttpGet("{touserId}/{msgCount}")]
        public async Task<ObjectRet<SendChatInfoRet>> SendChatCheck(long touserId, int msgCount)
        {
            return await chatMessageSvc.SendChatCheck(touserId, msgCount);
        }
    }
}
